NOTE
1.13 MySQL bin-log
1. What Is bin-log - A log at the MySQL Server layer, used for log archiving - Records change operations on the database, excluding query operations. 2. Why bin-log Is Still Needed When There Is redo-log - redo-log belongs to the InnoDB storage engine, while bin-log is at the MySQL Server layer 3. bin-log
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is bin-log
- A log at the MySQL Server layer, used for log archiving.
- Records change operations on the database, excluding query operations.
2. Why bin-log Is Still Needed When There Is redo-log
redo-logbelongs to the InnoDB storage engine, whilebin-logis at the MySQL Server layer.
3. Functions of bin-log
3.1. Backup and Recovery
- Generally, the complete MySQL database and bin-log are both backed up periodically.
- Find the most recent full backup of the complete database.
- Starting from the backup time point, retrieve the backed-up binlogs in sequence and replay them.
3.2. Primary-Replica Replication
4. bin-log File Location
- By default, it is placed in the data directory.
- Naming format:
mysql-bin.000001
5. bin-log Formats
5.1. statement Format
- Records the original SQL statements, such as insert/delete/update.
- Problem:
- It may cause inconsistency between primary and replica. For example, with
current_time, the time on the primary is time A, while when it reaches the replica it is time B.
- It may cause inconsistency between primary and replica. For example, with
5.2. row Format
- Records which record was modified and what the values were before and after the modification.
- For row insertion, the log records the new values of the related columns.
- For row deletion, the log marks that this row was deleted.
- For row update, the log records the new values of all columns.
- Problem:
- It takes up a lot of space. For example, when deleting 10,000 rows, with
statementonlydelete from t where id in (xxx)needs to be recorded, while withrow10,000 deleted rows need to be recorded.
- It takes up a lot of space. For example, when deleting 10,000 rows, with
5.3. mixed Format
- A mixture of the two formats above. MySQL uses
rowfor operations that may cause primary-replica inconsistency; otherwise it usesstatement.
5.4. Choosing a bin-log Format
rowis used in most cases because it records complete information and can be used for data recovery.
6. bin-log Write Mechanism

- During transaction execution, logs are first written to the binlog cache. When the transaction is committed, the binlog cache is written to the binlog file.
- The timing of
writeandfsyncis controlled by thesync_binlogparameter:- When
sync_binlog=0, each transaction commit only performswrite, notfsync. - When
sync_binlog=1, each transaction commit performswrite + fsync.- This is generally used for safety.
- When
sync_binlog=N (N>1), each transaction commit performswrite, butfsyncis performed only after N transactions have accumulated.
- When
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub